<!DOCTYPE html>
<html lang="zh-CN">
<head><!--头部-->
    <meta charset="utf-8">
    <meta name="keywords" content="关键词">
    <meta name="description" content="站点描述"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1,maximum-scale=1,user-scalable=no">
    <title>必应小条</title>
    <link href="css/main.css" rel="stylesheet">
    <script src="js/rem.js"></script>
    <script src="js/jquery-3.1.js"></script>
    <script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>

</head>
<body style="visibility:hidden;">
<div class="header-title"><!--标题-->
    <a href="javascript:history.back()" class="back"></a>
    <p class="title">必应小条</p>
</div>
<div class="main-cont"><!--内容 begin-->
    <div class="symbol-link"><!--图标-->
        <ul>
            <li>
                <a href="#" onclick="nextStep()"><img src="images/symbol01.png"><em>个人信息认证</em></a>
            </li>
            <li>
                <a href="#"><img src="images/symbol02.png"><em>运营商认证</em></a>
            </li>
            <!--		<li>
                        <a href="#"><img src="images/symbol03.png"><em>电商认证</em></a>
                    </li>-->
            <!--		<li>
                        <a href="#"><img src="images/symbol04.png"><em>银行卡</em></a>
                    </li>
                    <li>
                        <a href="#"><img src="images/symbol05.png"><em>芝麻授信</em></a>
                    </li>	-->
        </ul>
    </div>
    <div class="input-area"><!--信息填写-->
        <!--<div class="input-box">
            <span>姓名</span>
            <input type="text" id="name" placeholder="请填写您的姓名" />
        </div>
        <div class="input-box">
            <span>身份证</span>
            <input type="text" id="" placeholder="请填写您的身份证" />
        </div>	-->
        <div class="input-box input-box2">
            <span>借款金额</span>
            <input id="borrowMoney" type="text" placeholder="请选择您的借款金额(元)" readonly="readonly" class="jiner"/>
            <span class="s-btn"></span>
        </div>
        <div class="input-box input-box2">
            <span>借款周期</span>
            <input id="limitDays" type="text" placeholder="请选择您的借款周期(天)" readonly="readonly" class="zhouqi"/>
            <span class="s-btn"></span>
        </div>
        <div class="input-mask mask-jiner">
            <p class="p1">请选择借款金额</p>
            <ul>
                <li>1000</li>
                <!--	<li>2000元</li>
                    <li>3000元</li>
                    <li>4000元</li>-->
            </ul>
        </div>
        <div class="input-mask mask-zhouqi">
            <p class="p1">请选择借款周期</p>
            <ul>
                <li>7</li>
                <!--	<li>14天</li>
                    <li>21天</li>-->
            </ul>
        </div>
        <div class="input-box">
            <span>借款用途</span>
            <input id="borrowUsage" type="text" placeholder="请填写您的借款用途"/>
        </div>
    </div>
    <div class="result-info"><!--结果信息-->
        <ul>
            <li>
                <!--<p class="p1">8000元</p>-->
                <p id="resultMoney" class="p1">0元</p>
                <p class="p2">借款金额</p>
            </li>
            <li>
                <!--<p class="p1">80.2元</p>-->
                <p id="resultFee" class="p1">0元</p>
                <p class="p2">综合费用<a href="####" class="a-btn"></a></p>
            </li>
            <li>
                <!--<p class="p1">24天</p>-->
                <p id="resultDate" class="p1">0天</p>
                <p class="p2">借款期限</p>
            </li>
        </ul>
    </div>
    <div class="tijiao">
        <input onclick="submitIndex()" type="button" class="tj-btn" value="提交"/>
    </div>
</div><!--内容end-->
<script type="text/javascript" src="js/core.js"></script>
<script src="js/vconsole.min.js"></script>
<script>
    // init vConsole
/*    var vConsole = new VConsole();
    console.log('Hello world');*/
    var code = getvl("code");
    console.log(code);
</script>
<script>
    var userAuth=new Object();
    var ifAuth = getvl("auth");//是否已认证通过可借款 1 认证通过
    var clickTag = true;
    var isPay = true;//是否能借（第一次借或已还完订单）
    var borrowMoney;
    var limitDays;
    var userId = getvl("userId");
    $(function () {
        //1毫秒后显示页面
        setTimeout(showPage, 1);

        function showPage() {
            $('body').css({'visibility': 'visible'})
        }

        //金额选择弹出
        $('.input-area .jiner').click(function () {
            $('.input-area .mask-jiner').animate({'bottom': '0'}, 500);
        })
        $('.input-area .mask-jiner li').click(function () {
            var liTxt = $(this).html();
            $('.input-area .jiner').val(liTxt);
            borrowMoney = liTxt;
            $("#resultMoney").text(liTxt);
            $(this).parents('.mask-jiner').animate({'bottom': '-100%'}, 500)
        })
        //周期选择弹出
        $('.input-area .zhouqi').click(function () {
            $('.input-area .mask-zhouqi').animate({'bottom': '0'}, 500);
        })
        $('.input-area .mask-zhouqi li').click(function () {
            if ($('.input-area .jiner').val() == "") {
                alert("请先选择借款金额");
                return;
            }
            var liTxt = $(this).html();
            limitDays = liTxt;
            $('.input-area .zhouqi').val(liTxt);
            //已认证
            if (ifAuth == 1) {
                calcuFee();
            }
            $("#resultDate").text(liTxt);
            $(this).parents('.mask-zhouqi').animate({'bottom': '-100%'}, 500);
        })


        //获取openid
        getOpenId();

    });

    function getOpenId() {
        var code = getvl("code");
        if (code == "") {
            //不是从公众号打开，其他页面跳转过来的
        } else {
            $.ajax({
                url: urlcore + "/api/user/getOpenid?code=" + code,
                type: "get",
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    console.log("用户微信数据：",data.data);
                    //   alert("...."+ JSON.stringify(data.data));
                    if (data.success == true) {
                        if (data.data!=null) {
                            //更改认证状态
                            ifAuth=data.data.authStatus;
                            userId=data.data.id;
                            if(data.data.userAuth!=null){
                                userAuth=data.data.userAuth;
                            }
                            // toLogin(data.data.phone, data.data.password);
                        }
                    } else {
                        console.log('返回错误');
                    }
                }, error: function () {
                    console.log("error");
                }
            });

        }
    }

    //计算费用
    function calcuFee() {
//            var userMoney=$("#borrowMoney").val();
//            var limitDays=$("#limitDays").val();
        var order = {
            userId: userId,
            userMoney: borrowMoney,
            limitDays: limitDays
        };
        $.ajax({
            type: "GET",
            url: urlcore + "/api/user/userFirstPage",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: order,
            success: function (data) {
                console.log("计算综合费用、检查有无未还款订单返回：", data.data);
                var cbdata = data.data;
                if (cbdata.isPay == "true") {
                    $("#resultFee").text(cbdata.allWasteMoney);
                } else {
                    /*     borrowMoney:1000
                         gmtDatetime:1511942180000
                         isPay:"false"
                         limitPayTime:1512489600000
                         needPayMoney:1000
                         orderId:2832*/
                    isPay = false;
                    alert("你还有订单未还款,待还款金额为" + cbdata.needPayMoney + "元，请还款后再借款")
                }

            },
            error: function () {
                alert("error")
            }
        });
    }


    //提交订单
    function submitIndex() {
        if (!isPay) {
            alert("你还有订单未还款，请还款后再借款");
            return;
        }
        if ($("#borrowUsage").val() == "" || $("#borrowMoney").val() == "" || $("#limitDays").val() == "") {
            alert("请完善借款信息");
            return;
        }
        if (clickTag) {
            clickTag = false;
            console.log(ifAuth);
            if (ifAuth == 1) {
                alert("正在提交中，请等待");
                var borrowMoney = $("#borrowMoney").val();
                var limitDays = $("#limitDays").val();
                var userId = getvl("userId");//用户id
                var borrowUsage = $("#borrowUsage").val();
                var order = {
                    borrowMoney: borrowMoney,
                    limitDays: limitDays,
                    userId: userId,
                    borrowUsage: borrowUsage
                };
                $.ajax({
                    type: "GET",
                    url: urlcore + "/api/loanOrder/addNewOrder",
                    dataType: "json",
                    contentType: "application/json;charset=UTF-8",
                    data: order,
                    success: function (data) {
                        if (data.success == true) {
                            console.log("返回数据：", data.data);
                            var cbdata = data.data;
                            var id = cbdata.id;
                            var postData = {
                                orderId: id,
                                userId: userId
                            }
                            $.ajax({
                                type: "GET",
                                url: urlcore + "/api/loanOrder/confirmOrder",
                                dataType: "json",
                                contentType: "application/json;charset=UTF-8",
                                data: postData,
                                success: function (data) {
                                    console.log("第二步验证返回：", data.data);
                                    var cbdata = data.data;
                                    clickTag = true;
                                    if (cbdata.isPay == "false") {
                                        alert("您有订单未还款尚未还款！")
                                    } else {
                                        alert("提交成功");
                                    }

                                },
                                error: function () {
                                    alert("error")
                                }
                            });
                        } else alert(data.msg)


                    },
                    error: function () {
                        clickTag = true;
                        alert("error")
                    }
                });
            } else {
                clickTag = true;
                alert("请先完成认证");
                nextStep()

            }
        } else {
            alert("请等待")
        }

    }

    //下一步 是跳转认证还是可直接借款
    function nextStep() {
        if(ifAuth==1){
            alert("你已认证，可直接借款");
        }else{
            if(userAuth.baiscAuth==0||userAuth.identityAuth==0){
                //去身份认证
                window.location.href = "page1.html?userId="+userId;
            }else if(userAuth.phoneAuth==0){
                //去运营商认证
                window.location.href = "page2.html?userId="+userId;
            }

        }
    }
</script>


<script>


</script>
</body>
</html>
